/*
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-29 11:08:49
 * @Description  : 用户相关接口
 */

import { TOKEN } from '@/constants'
import type { SpaceChartRes } from '@/types/overview'
import type {
  UserLogin,
  UserRegister,
  UserProfileReq,
  UserValidate,
  UserProfile,
  FindPwdReq,
  SetStatusUser,
  TokenInfo,
  UserNotDepReq,
  UserNotDepPageRes,
  UserReq,
  SetUserRoleReq,
  SetUserSizeMaxReq,
  UserOptionsRes,
  UserOptionReq,
  SendEmailReq
} from '@/types/user'
import request from '@/utils/request'
import { getItem } from '@/utils/storage'
import type { AxiosRequestConfig } from 'axios'

// 用户注册接口
export const userRegister = (data: UserRegister) => {
  return request.post('/user/register', data)
}

// 用户登录接口
export const userLogin = (data: UserLogin) => {
  return request.post<TokenInfo>('/user/login', data)
}

// 用户退出登录接口
export const userLoginOut = () => {
  return request.post('/user/loginout')
}

// 用户刷新token接口
export const userReflesh = () => {
  return request.get<TokenInfo>('/user/reflesh', {
    headers: {
      refleshToken: 'Bearer ' + getItem(TOKEN)?.reflesh_token || ''
    }
  })
}

// 用户个人信息查询接口
export const getUserProfile = () => {
  return request.get<UserProfile>('/user/info')
}

// 用户个人信息更新接口
export const updateUserProfile = (
  data: UserProfileReq,
  config?: AxiosRequestConfig
) => {
  return request.put('/user/infoUpdate', data, config)
}

// 验证用户信息是否正确接口
export const validateUserProfile = (data: UserValidate) => {
  return request.post('/user/checkUserInfo', data)
}

// 用户获取自己的空间使用统计接口
export const getUserSpaceChart = () => {
  return request.get<SpaceChartRes>('/user/settings/spaceChart')
}

// 给用户发送验证码接口
export const sendEmailCode = (data: SendEmailReq) => {
  return request.post('/user/sendCode', data)
}

// 用户密码找回接口
export const findPwd = (data: FindPwdReq) => {
  return request.put('/user/findPwd', data)
}

// 给用户发送图形验证码接口
export const sendPicValidCode = () => {
  return request.get<{
    img: string
  }>('/user/slideCode')
}

// --------------------------------------管理员端---------------------------------------------------------

// 批量启用/禁用用户
export const setUserStatus = (data: SetStatusUser) => {
  return request.put('/user/status', data)
}

// 批量设置用户的系统角色
export const setUserRole = (data: SetUserRoleReq) => {
  return request.put('/user/setRole', data)
}

// 批量设置用户的个人空间大小
export const setUserSizeMax = (data: SetUserSizeMaxReq) => {
  return request.put('/user/setSizeMax', data)
}

// 批量删除用户接口
export const delUser = (userIds: string[], picCode?: string) => {
  return request.delete('/user/delete', { data: { userIds, picCode } })
}

// 获取所有不属于任何部门的用户接口
export const getUsersNotDep = (params?: UserNotDepReq) => {
  return request.get<UserNotDepPageRes>('/user/notDep/list', { params })
}

// 获取指定部门下的用户
export const getUsers = (params?: UserReq) => {
  return request.get<UserNotDepPageRes>('/user/list', { params })
}

// 获取用户的权限列表
export const getUserPermissions = () => {
  return request.get<string[]>('/user/permission/list')
}

// 获取用户的配置列表
export const getUserOptions = () => {
  return request.get<UserOptionsRes>('/user/settings/list')
}

// 更新用户的个人配置
export const setUserOption = (data: UserOptionReq) => {
  return request.post('/user/settings/update', data)
}
